<template>
    <popup :title="title" @onsubmit="submit">
      <el-form :model="formData" label-width="100px">
        <el-form-item label="器械商名称" prop="name">
          <el-input v-model="formData.name" />
        </el-form-item>
        
        <el-form-item label="省市区">
          <el-cascader
            v-model="selectedArea"
            :options="areaOptions"
            @change="handleAreaChange"
          />
        </el-form-item>
        
        <el-form-item label="头像">
          <single-upload v-model="formData.avatar" />
        </el-form-item>
        
        <el-form-item label="成立日期">
          <el-date-picker
            v-model="formData.found"
            type="date"
            value-format="YYYY-MM-DD"
          />
        </el-form-item>
      </el-form>
    </popup>
  </template>
  
  <script setup>
import { ref } from "vue";
import { 
  addMedicalUserAPI, 
  updateMedicalUserAPI,
  getMedicalUserDetailAPI // 确保导入这个API
} from "@/network/medicaluser";

const emits = defineEmits(['success']); // 确保声明emits
  const formData = ref({
    name: "",
    provinceId: null,
    cityId: null,
    districtId: null,
    avatar: "",
    found: "",

  });
  
  const submit = async () => {
    if (formData.value.medicalId) {
      await updateMedicalUserAPI(formData.value);
    } else {
      await addMedicalUserAPI(formData.value);
    }
    emits("success");
  };
  
  defineExpose({
  open: async (id) => {  // 这里添加 async
    if (id) {
      formData.value = await getMedicalUserDetailAPI(id);
    }
    popupRef.value?.open();
  }
});
  </script>